package rtda

type JStack struct {
	maxSize uint
	size    uint
	_top    *JFrame
}

func newJStack(maxSize uint) *JStack {
	return &JStack{maxSize: maxSize}
}

func (js *JStack) push(jFrame *JFrame) {
	if js.size >= js.maxSize {
		panic("java.lang.StackOverFlowError")
	}
	if js._top != nil {
		jFrame.lower = js._top
	}
	js._top = jFrame
	js.size++
}

func (js *JStack) pop() *JFrame {
	if js._top == nil {
		panic("jvm stack is empty!")
	}
	top := js._top
	js._top = top.lower
	top.lower = nil
	js.size--
	return top
}

func (js *JStack) top() *JFrame {
	if js._top == nil {
		panic("jvm stack is empty!")
	}
	return js._top
}

func (js *JStack) isEmpty() bool {
	return js._top == nil
}

func (js *JStack) clear() {
	for !js.isEmpty() {
		js.pop()
	}
}

func (js *JStack) getFrames() []*JFrame {
	jFrames := make([]*JFrame, 0, js.size)
	for jFrame := js._top; jFrame != nil; jFrame = jFrame.lower {
		jFrames = append(jFrames, jFrame)
	}
	return jFrames
}
